/**
 * 2019年2月17日下午1:09:32
 * @author 谢进财
 */
package com.baihang.project.ds.dao;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baihang.framework.dao.BaseDao;
import com.baihang.framework.dao.Record;
import com.baihang.framework.kit.StrKit;



/**
 * @author whati
 *
 */
@Repository
public class MySqlRedisDao extends BaseDao {
	
	public boolean batchUpdateFangYuanList(List<String> list) {
		String sql = "update room set E_CollectionCount=?,D_state=?,D_Monitor=?,D_Operation=?,D_OperationRemark=?,D_SaleDate=?,D_SerialNumber=? where D_ID=?";
		super.getJdbcTemplate().batchUpdate(sql, new BatchPreparedStatementSetter() {
			public int getBatchSize() {
				return list.size();
				// 这个方法设定更新记录数，通常List里面存放的都是我们要更新的，所以返回list.size();
			}
			public void setValues(PreparedStatement ps, int i) throws SQLException {
				JSONObject jsonObject = JSON.parseObject(list.get(0));
				ps.setInt(1, jsonObject.getIntValue("E_CollectionCount"));
				ps.setInt(2, jsonObject.getIntValue("D_state"));
				ps.setInt(3, jsonObject.getIntValue("D_Monitor"));	
				ps.setString(4, jsonObject.getString("D_Operation"));
				ps.setString(5, jsonObject.getString("D_OperationRemark"));
				ps.setString(6, jsonObject.getString("D_SaleDate"));
				ps.setString(7, jsonObject.getString("D_SerialNumber"));
				ps.setInt(8, jsonObject.getIntValue("D_ID"));
			}
		});

		return true;
	}
	
	@Transactional
	public boolean batchInsertUserCollectionList(List<Record> list) {
		String sql = "insert into t_collection (UserName,FieldId,Sort) values (?,?,?)";
		super.getJdbcTemplate().batchUpdate(sql, new BatchPreparedStatementSetter() {
			public int getBatchSize() {
				return list.size();
				// 这个方法设定更新记录数，通常List里面存放的都是我们要更新的，所以返回list.size();
			}

			public void setValues(PreparedStatement ps, int i) throws SQLException {
				Record  rec = list.get(i);
				ps.setString(1, rec.getStr("UserName"));					
				ps.setInt(2, rec.getInt("FieldId"));
				ps.setInt(3, rec.getInt("Sort"));			
			}
		});

		return true;
	}
}
